﻿namespace PorteringManagement.Web.Controllers
{
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Web.Mvc;
    using PorteringManagement.Model;

    [Authorize(Roles = "Admin")]
    public class CargoController : Controller
    {
        private readonly PorteringManagementContext context = new PorteringManagementContext();

        // GET: /Cargo/
        public ViewResult Index()
        {
            var cargos = this.context.Cargos
                .Include(c => c.CargoType)
                .Include(c => c.Customer)
                .Include(c => c.Itinerary);

            return this.View(cargos.ToList());
        }

        // GET: /Cargo/Details/5
        public ViewResult Details(int id)
        {
            var cargo = this.context.Cargos.Find(id);

            return this.View(cargo);
        }

        // GET: /Cargo/Create
        public ActionResult Create()
        {
            this.ViewBag.CargoTypeID = new SelectList(this.context.CargoTypes, "CargoTypeID", "Name");
            this.ViewBag.CustomerID = new SelectList(this.context.Customers, "CustomerID", "Name");

            return View();
        } 

        // POST: /Cargo/Create
        [HttpPost]
        public ActionResult Create(Cargo cargo)
        {
            if (this.ModelState.IsValid)
            {
                this.context.Cargos.Add(cargo);
                this.context.SaveChanges();

                return this.RedirectToAction("Index");  
            }

            this.ViewBag.CargoTypeID = new SelectList(this.context.CargoTypes, "CargoTypeID", "Name", cargo.CargoTypeID);
            this.ViewBag.CustomerID = new SelectList(this.context.Customers, "CustomerID", "Name", cargo.CustomerID);

            return this.View(cargo);
        }
        
        // GET: /Cargo/Edit/5
        public ActionResult Edit(int id)
        {
            var cargo = this.context.Cargos.Find(id);
            this.ViewBag.CargoTypeID = new SelectList(this.context.CargoTypes, "CargoTypeID", "Name", cargo.CargoTypeID);
            this.ViewBag.CustomerID = new SelectList(this.context.Customers, "CustomerID", "Name", cargo.CustomerID);
            this.ViewBag.ItineraryID = new SelectList(this.context.Itineraries, "ItineraryID", "Name", cargo.ItineraryID);

            return this.View(cargo);
        }

        // POST: /Cargo/Edit/5
        [HttpPost]
        public ActionResult Edit(Cargo cargo)
        {
            if (this.ModelState.IsValid)
            {
                this.context.Entry(cargo).State = EntityState.Modified;
                this.context.SaveChanges();

                return RedirectToAction("Index");
            }

            this.ViewBag.CargoTypeID = new SelectList(this.context.CargoTypes, "CargoTypeID", "Name", cargo.CargoTypeID);
            this.ViewBag.CustomerID = new SelectList(this.context.Customers, "CustomerID", "Name", cargo.CustomerID);
            this.ViewBag.ItineraryID = new SelectList(this.context.Itineraries, "ItineraryID", "Name", cargo.ItineraryID);

            return this.View(cargo);
        }

        // GET: /Cargo/Delete/5
        public ActionResult Delete(int id)
        {
            var cargo = this.context.Cargos.Find(id);

            return this.View(cargo);
        }

        // POST: /Cargo/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            var cargo = this.context.Cargos.Find(id);
            this.context.Cargos.Remove(cargo);
            this.context.SaveChanges();

            return this.RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            this.context.Dispose();
            base.Dispose(disposing);
        }
    }
}